﻿//        Another Demo from Andy L. & MissedMemo.com
// Borrow whatever code seems useful - just don't try to hold
// me responsible for any ill effects. My demos sometimes use
// licensed images which CANNOT legally be copied and reused.

using System;
using System.Windows.Data;

using MissedMemo.MeatAndCheeseDisc.UIHelperClasses;
using MissedMemo.MeatAndCheeseDisc.OrderEntryService.OrderProcessing;


namespace MissedMemo.MeatAndCheeseDisc.OrderEntry
{
    public class OrderStateToStringConverter : IMultiValueConverter
    {
        public object Convert( object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            string strOrderStatus = "Error";

            // Test any MultiValueConverter params for invalid DESIGN TIME values
            // (in this case, null ElapsedTime triggers MainWindow design-time crash)

            if( values[0] is TimeSpan )
            {
                ProcessingState state = (ProcessingState)values[1];

                if( state == ProcessingState.Scheduled )
                {
                    DateTime dtScheduled = (DateTime)values[2];

                    if( dtScheduled != null )
                        strOrderStatus = string.Format( "Scheduled: {0:M/d h:mm tt}", dtScheduled );
                }

                else if( state == ProcessingState.Completed )
                {
                    Nullable<DateTime> dtCompleted = (Nullable<DateTime>)values[3];

                    if( dtCompleted != null )
                        strOrderStatus = string.Format( "Completed: {0:M/d h:mm tt}", dtCompleted.Value );
                }

                else
                {
                    TimeSpan elapsedTime = (TimeSpan)values[0];
                    string strTimeElapsed = string.Format( "{0:mm:ss}", new DateTime( elapsedTime.Ticks ) );

                    if( state == ProcessingState.Cooking )
                        strOrderStatus = strTimeElapsed + "  Cooking...  ";

                    else if( state == ProcessingState.OutForDelivery )
                    {
                        string strDeliveryDriver = values[5] as string;

                        if( strDeliveryDriver != null )
                            strOrderStatus = string.Format( "{0}  Delivering... ({1})", strTimeElapsed, strDeliveryDriver );
                    }

                    else if( state == ProcessingState.Ready )
                    {
                        bool deliveryRequested = (bool)values[4];
                        strOrderStatus = string.Format( "{0}  Ready for {1}", strTimeElapsed, deliveryRequested ? "Delivery" : "Pickup" );
                    }
                }
            }
            
            return strOrderStatus;
        }


        public object[] ConvertBack( object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture )
        {
            throw new NotImplementedException();
        }
    }
}
